package com.coder.oj.admin.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.oj.admin.dto.CaseDto;
import com.coder.oj.admin.dto.KeyValueDto;
import com.coder.oj.admin.mapstruct.CaseMapstruct;
import com.coder.oj.admin.query.CaseQuery;
import com.coder.oj.admin.service.CaseService;
import com.coder.oj.admin.vo.CaseVo;
import com.coder.oj.admin.vo.KeyValueVo;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.JwtToken;
import com.coder.oj.common.core.R;
import com.coder.oj.common.entity.Case;
import com.coder.oj.common.mapper.CaseMapper;
import com.coder.oj.common.validate.ValidationGroups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Bosen 2022/11/25 15:52
 **/
@Api(tags = {"样例管理"})
@RestController
@RequestMapping("/case")
public class CaseController {

    @Autowired
    private CaseService caseService;

    @Autowired
    private CaseMapper caseMapper;

    @Autowired
    private CaseMapstruct caseMapstruct;

    @JwtToken
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public R<JsonPage<CaseVo>> getCasePage(@RequestBody CaseQuery params) {
        return R.ok().setResult(caseService.getCasePage(params));
    }

    @JwtToken
    @PostMapping
    @ApiOperation("新增样例")
    public R saveCase(@Validated(ValidationGroups.Insert.class) @RequestBody CaseDto params) {
        caseMapper.insert(caseMapstruct.toEntity(params));
        return R.ok();
    }

    @JwtToken
    @ApiOperation("样例详情")
    @GetMapping
    public R<CaseVo> getCaseById(@RequestParam("id") Long id) {
        return R.ok().setResult(caseMapstruct.toVO(caseMapper.selectById(id)));
    }

    @JwtToken
    @PutMapping
    @ApiOperation("修改样例")
    public R updateCase(@Validated(ValidationGroups.Update.class) @RequestBody CaseDto params) {
        caseMapper.updateById(caseMapstruct.toEntity(params));
        return R.ok();
    }

    @JwtToken
    @DeleteMapping
    @ApiOperation("删除样例")
    public R deleteCaseById(@RequestParam("id") Long id) {
        caseMapper.deleteById(id);
        return R.ok();
    }

    @JwtToken
    @GetMapping("/list")
    @ApiOperation("样例列表")
    public R<List<KeyValueVo>> getCaseListByProjectId(@RequestParam("projectId") Long projectId) {
        return R.ok().setResult(caseService.getCaseListByProjectId(projectId));
    }
}
